Type Coercion

Example

console.log(1 == '1');

This is true.

console.log(1 === '1');

This is false.

Example of JS Coercion

Example 1:

console.log(1 == '1');

Here the coercion converts the type to string or number, so the results is true.

Example 2:

if (1) {
  console.log('1 is True);
}

Here the coercion converts the type of 1 to true.

Example 2:

if (0) {
  console.log('0 is False);
}

Here the coercion converts the type of 0 to false.

Exercise

try to guess the outputs.

false == '';
false == [];
false == {};
'' == 0;
'' == [];
'' == {};
0 == [];
0 == {};
0 == null;